<?php
	session_start ();
	require_once './php_lib/conexion.php';
	require_once('./php_lib/funciones.php');

	$rut = "11987646";
	
	if(isset($_POST['prod'])){
		$item = "SELECT prod_id, col_descripcion, talla_num, mod_descripcion, prod_valor
				FROM Producto,Color,Talla,Modelo
				WHERE Producto.col_id = Color.col_id
				AND Producto.col_id = Color.col_id
				AND Producto.talla_id = Talla.talla_id
				AND Producto.mod_id = Modelo.mod_id
				AND prod_id=".$_POST['prod'];
				
		$qi = mysql_query($item, $db_zapatos)or die(mysql_error());
		$lista = mysql_fetch_assoc($qi);
		$lista_e = null;
	
		if(isset($_POST['lista'])){
			$lista_e= unserialize(stripslashes($_POST['lista']));
		}
	        if(isset($_GET['d'])){
			$lista_e= unserialize(stripslashes($_GET['d']));
			echo $lista_e;
		}

		if(mysql_num_rows($qi)!=0){
			
			$cant = "SELECT `pros_stock` FROM `Pro_Sec`
				WHERE `prod_id`=".$_POST['prod']."
				AND `sec_id` IN (SELECT `sec_id` FROM `Sector`
				WHERE `bod_id` IN (SELECT `bod_id` FROM Bodega
				WHERE suc_id = (SELECT suc_id FROM Personal
				WHERE per_rut = ".$rut.")))";
				
			$qc = mysql_query($cant, $db_zapatos)or die(mysql_error());
			
			if(mysql_num_rows($qc)!=0){
			
				$qc_r = mysql_fetch_assoc($qc);
			
				if($_POST['cant2']<=$qc_r['pros_stock'] && $_POST['cant2']!=0){
					$lista['cant'] = $_POST['cant2'];
					$lista['subt'] = $lista['prod_valor']*$lista['cant'];	
					$lista_e[]=$lista;
				}
				else{
					echo "<script language='JavaScript'>
					alert('Cantidad erronea')		
					</script>";
				}
			}
			else{
				echo "<script language='JavaScript'>
				alert('Producto no registrado en la sucursal')		
				</script>";
			}
		}
		else{
			echo "<script language='JavaScript'>
			alert('No existe el Producto')		
			</script>";
		}
		
}
?>
<link href="css/estilos.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.js"></script>
<h3><em> ID del producto </em>
</h3>
<form id="form1" name="form1" method="post" action=""> 
  <p>
    <label for="prod"></label>
    <input type="text" name="prod" id="prod" />
    <input type="button" name="button1" id="button1" value="Buscar"/>
    <label for="cant2">Cantidad</label>
    <input name="cant2" type="text" id="cant2" value="1"/>
    <input type="hidden" name="lista" value='<?php
	if(isset($lista_e)){
	 echo serialize($lista_e);
	}
	?>' />
  </p>
  </form>
  <p>
  <form id="form2" name="form2" method="post" action="">
<table width="500" border="1" cellpadding="1" id="n" class="db_listado">
    <tr>
    <th width="68">Producto</th>
    <th width="44">Color</th>
    <th width="47">Talla</th>
    <th width="55">Modelo</th>
    <th width="57">Valor</th>
    <th width="66">Cantidad</th>
    <th width="71">Subtotal</th>
  </tr>
  <?php 
  if(isset($lista_e) && $lista_e!=null){
	  $suma =0;
	  $num=0;
	  	foreach($lista_e as $p){ ?>
  <tr>
    <td><?php echo $p['prod_id']; ?></td>
    <td><?php echo $p['col_descripcion']; ?></td>
    <td><?php echo $p['talla_num']; ?></td>
    <td><?php echo $p['mod_descripcion']; ?></td>
    <td><?php echo $p['prod_valor']; ?></td>
    <td><?php echo $p['cant'];?></td>
    <td id="subt"><?php echo $p['subt'];
	$suma = $suma + $p['subt'];?></td>
<!--    <td><a href="#" class="ico_del" title="Eliminar" </a></td>-->
  </tr>
  <?php	$num++;}
  }?>
</table>
<p>
  <input type="hidden" name="lista2" value='<?php
	if(isset($lista_e)){
	 echo serialize($lista_e);
	}
	?>' />    
  <input type="hidden" name="rut" value='<?php
	 echo $rut;
	?>' />
<table width="125" border="1" cellpadding="10" class="db_listado">
    <tr>
      <td width="45">Total  $</td>
      <td width="34" id="suma"><?php if(isset($suma))
	   echo $suma;?></td>
    </tr>
</table>
<p>
<input type="button" name="acep" id="acep" value="Realizar Venta"/> 
</form>

<script language="javascript">
$('#button1').click(function() {
	
	if($('#prod').val()==""){
		alert("Ingrese id del Producto");
	}
	else{		
	$.ajax({
	type: 'POST',
	url: 'realizar_venta.php',
	data: $('#form1').serialize(),
	// Mostramos un mensaje con la respuesta de PHP
	success: function(data) {
		$('#mainDiv').html(data);
	}})
	}
});
$('#acep').click(function() {
	// Enviamos el formulario usando AJAX
	$.ajax({
	type: 'POST',
	url: 'venta.php',
	data: $('#form2').serialize(),
	// Mostramos un mensaje con la respuesta de PHP
	success: function(data) {
		$('#mainDiv').html(data);
	}})        
});

</script>